/**
 * Created by qqlai on 2016/11/22.
 */
{
    angular.module('emptyBedModule')
        .controller('EmptyBedController', EmptyBedController);
    EmptyBedController.$inject = ['http.interfaces', '$state'];
    function EmptyBedController(interfaces, $state) {
        let vm = this;
        let collect = {
            Changed: Changed,
            getDormId: getDormId,
            allfreeBedCount: allfreeBedCount,
            departStatfreeBedCount: departStatfreeBedCount,
            active: Active,
            getInterfacesData: getInterfacesData,
            datasz: [],
            List: [],
            dormStat: [],
            selected: {},
            logout:logout
        };
        collect.active();//进入控制器后调用一次
        vm.collect = collect;//输出集合，提供给模板调用

        /** Active方法
         * @desc 控制器触发时执行一次的方法
         * @constructor
         */
        function Active() {
                getInterfacesData();
                collect.selected = collect.List[0];
                allfreeBedCount();
        }

        function getDormId(id, dormName) {
            $state.go('tab.dormitoryBuilding2', {id: id});
            interfaces.dormName = dormName;
        }

        function getInterfacesData() {
            interfaces.departmentList().then(res=> {
                if (res.data.resultCode == 1) {
                    collect.dormitoryList = res.data.data;

                    collect.List = res.data.data;

                    collect.List.unshift({
                        "departName": "全校",
                        "departCode": 0,
                        "departId": 1
                    },);
                    if (collect.List.length > 0) {
                        collect.selected = collect.List[0];
                    }
                }
            });

            interfaces.dormitoryList().then(res=> {
                if (res.data.resultCode == 1) {
                    collect.dormitoryList = res.data.data;
                }
            });

            interfaces.dormStat().then(res=> {
                if (res.data.resultCode == 1) {
                    collect.dormStat = res.data.data;
                }
            });
        }

        function allfreeBedCount() {
            interfaces.departStat().then(res=> {

                if (res.data.result == 1) {
                    collect.departStat = res.data.data;
                    var myChart = echarts.init(document.getElementById('kcw1'));
                    // 指定图表的配置项和数据
                    var option = {
                        tooltip: {
                            trigger: 'item',
                            formatter: "{a} <br/>{b}: {c} ({d}%)"
                        },
                        legend: {
                            orient: 'vertical',
                            x: 'left',
                            data: ['空床位', '已住']
                        },
                        title: {
                            text: '',
                            x: 'center',
                            y: 'center',
                            textStyle: {
                                fontSize: 14,
                                color: '#aaa'
                                // height:30,
                                // lineHeight:30
                            },
                            lineHeight: 60
                        },
                        // Axis: {
                        //     type: 'category',
                        //     data:['空床位','已住']
                        // },
                        series: [

                            {
                                name: '空床位',
                                type: 'pie',
                                radius: ['40%', '55%'],
                                avoidLabelOverlap: false,
                                data: []
                            }
                        ],
                        color: ['rgb(239,169,94)', '#cb3428', 'rgb(204,204,204)', 'rgb(221,164,144)']

                    };
                    option.series[0].data.push({value: collect.departStat.freeBedCount, name: '空床位'});
                    option.series[0].data.push({value: collect.departStat.allLodgerCount, name: '已住'});
                    option.title.text = "共置空床位" + '\n' + collect.departStat.freeBedCount + '个' + '\n' + '\n' + '已住' + '\n' + collect.departStat.allLodgerCount + '个';
                    myChart.setOption(option);


                }
            });
        }

        function departStatfreeBedCount() {
            interfaces.departStat({departId: collect.selected.departId}).then(res=> {
                if (res.data.result == 1) {
                    collect.departStat = res.data.data;
                    var myChart = echarts.init(document.getElementById('kcw1'));
                    // 指定图表的配置项和数据
                    var option = {
                        tooltip: {
                            trigger: 'item',
                            formatter: "{a} <br/>{b}: {c} ({d}%)"
                        },
                        legend: {
                            orient: 'vertical',
                            x: 'left',
                            data: ['空床位', '已住']
                        },
                        title: {
                            text: '',
                            x: 'center',
                            y: 'center',
                            textStyle: {
                                fontSize: 14,
                                color: '#aaa'

                            },
                            // height:30,
                            lineHeight: 30
                        },
                        // Axis: {
                        //     type: 'category',
                        //     data:['空床位','已住']
                        // },
                        series: [

                            {
                                name: '空床位',
                                type: 'pie',
                                radius: ['40%', '55%'],
                                avoidLabelOverlap: false,

                                data: []
                            }
                        ],
                        color: ['rgb(239,169,94)', 'rgb(191,85,93)', 'rgb(204,204,204)', 'rgb(221,164,144)']

                    };
                    option.series[0].data.push({value: collect.departStat.freeBedCount, name: '空床位'});
                    option.series[0].data.push({value: collect.departStat.allLodgerCount, name: '已住'});
                    option.title.text = "共置空床位" + '\n' + collect.departStat.freeBedCount + '个' + '\n' + '\n' + '已住' + '\n' + collect.departStat.allLodgerCount + '个';
                    myChart.setOption(option);
                }
            });
        }

        function Changed() {
            if (collect.selected.departId == 1) {
                allfreeBedCount();
                interfaces.dormStat().then(res=> {
                    if (res.data.resultCode == 1) {
                        collect.dormStat = res.data.data;
                    }
                });
            } else {
                departStatfreeBedCount();
                interfaces.dormStat({departId:collect.selected.departId}).then(res=> {
                    if (res.data.resultCode == 1) {
                        collect.dormStat = res.data.data;
                    }
                });
            }
        }
        function logout() {
            interfaces.logout();
        }
    }
}